/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuyensinh;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author user
 */
public class ThiSinh {

    static Connection conn;
    static Statement stm;
    String sbd;
    String hoten;
    String ngaysinh;
    String maNganh;
    String makhuvuc;
    String madoituong;

    public ThiSinh() {
    }

    public ThiSinh(String sbd, String hoten, String ngaysinh, String manganh, String makhuvuc, String madoituong) {
        this.sbd = sbd;
        this.hoten = hoten;
        this.ngaysinh = ngaysinh;
        this.maNganh = manganh;
        this.makhuvuc = makhuvuc;
        this.madoituong = madoituong;
    }

    public static void openDatabase() throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:tuyensinh");
        stm = conn.createStatement();

    }

    public static void closeDatabase() throws SQLException {
        stm.close();
        conn.close();
    }

    public void insThiSinh() throws SQLException {
        openDatabase();
        String query = "insert into ThiSinh values('" + sbd + "', '" + hoten + "', '" + ngaysinh + "', '"
                + maNganh + "', '" + makhuvuc + "', '" + madoituong + "')";
        stm.executeUpdate(query);
        closeDatabase();
    }

    public String getSbd() {
        return sbd;
    }

    public static ThiSinh findThiSinhBySbd(String sbd) throws SQLException {
        openDatabase();
        String query = "select * from ThiSinh where sbd='" + sbd + "'";
        ResultSet rs = stm.executeQuery(query);
        ThiSinh ts = null;
        if (rs.next()) {
            ts = new ThiSinh();
            ts.setSbd(sbd);
            ts.sethoten(rs.getString("hoten"));
            ts.setNgaysinh(rs.getString("ngaysinh"));
            ts.setMaNganh(rs.getString("manganh"));
            ts.setMakhuvuc(rs.getString("makhuvuc"));
            ts.setMadoituong(rs.getString("madoituong"));
        }
        return ts;
    }

    public static ArrayList<ThiSinh> findThiSinhByTen(String ten) throws SQLException {
        ArrayList<ThiSinh> result = new ArrayList<>();
        openDatabase();
        String query = "select * from ThiSinh where hoten like '%" + ten + "%'";
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
            do {
                ThiSinh ts = new ThiSinh();
                ts.setSbd(rs.getString("sbd"));
                ts.sethoten(rs.getString("hoten"));
                ts.setNgaysinh(rs.getString("ngaysinh"));
                ts.setMaNganh(rs.getString("manganh"));
                ts.setMakhuvuc(rs.getString("makhuvuc"));
                ts.setMadoituong(rs.getString("madoituong"));
                result.add(ts);
            } while (rs.next());
        }
        return result;
    }

    public String getHoten() {
        return hoten;
    }

    public String getNgaysinh() {
        return ngaysinh;
    }

    public String getMakhuvuc() {
        return makhuvuc;
    }

    public String getMadoituong() {
        return madoituong;
    }

    public void setSbd(String sobd) {
        sbd = sobd;
    }

    public void sethoten(String hten) {
        hoten = hten;
    }

    public void setNgaysinh(String ngsinh) {
        ngaysinh = ngsinh;
    }

    public void setMakhuvuc(String makhvuc) {
        makhuvuc = makhvuc;
    }

    public void setMadoituong(String madtuong) {
        madoituong = madtuong;
    }

    public String getMaNganh() {
        return maNganh;
    }

    public void setMaNganh(String maNganh) {
        this.maNganh = maNganh;
    }

    public static void updateThiSinhInfo(String sbd, String hoten, String ngaysinh, String manganh, String makhuvuc, String madoituong) throws SQLException {
        openDatabase();
        String query = "update ThiSinh set hoten='" + hoten + "', ngaysinh='"
                + ngaysinh + "', manganh='" + manganh + "', makhuvuc='" + makhuvuc + "', madoituong='" + madoituong + "' "
                + " where sbd='" + sbd + "'";
        stm.executeUpdate(query);
        closeDatabase();
    }

    public static void main(String[] args) {
        ThiSinh ts = new ThiSinh("123", "45", "67", "89", "1000", "1001");
        try {
            ts.insThiSinh();
        } catch (SQLException ex) {
            Logger.getLogger(ThiSinh.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
